#!/bin/bash

######## Box for Config ##########


######## End Box Config ##########


# Docs:
# Bash is global watch script, no matter when define function, in same file scope is ok, like golang, not like clang


# Docs:
# We don't need return code for "$(command)",only stdout is needed
# Allow `[[ -n "$(command)" ]]`, `func "$(command)"`, pipes, etc(make sure your bash support)
# 
# shellcheck disable=SC2312
#

set -u

abort() {
	printf "%s\n" "$@" >&2
	exit 1
}

# Docs:
# Fail fast with a concise message when not using bash
# Single brackets are needed here for POSIX compatibility
#
# shellcheck disable=SC2292
if [ -z "${BASH_VERSION:-}" ]; then
	abort "Bash is required to interpret this script, no fish, no zsh"
fi

# Docs:
# Check if script is run with force-interactive mode in CI
if [[ -n "${CI-}" && -n "${INTERACTIVE-}" ]]; then
	abort "Can not run force-interactive mode in CI"
fi

# Docs:
# Check if both `INTERACTIVE` and `NONINTERACTIVE` are set
# Always use single-quoted strings with `exp` expressions
# shellcheck disable=SC2016
if [[ -n "${INTERACTIVE-}" && -n "${NONINTERACTIVE-}" ]]; then
	abort 'Both `${INTERACTIVE` and `$NONINTERACTIVE` are set. Please unset at least one variable and try again'
fi


# Docs:
# check if script is run in POSIX mode
if [[ -n "${POSIXLY_CORRECT+1}" ]]; then
	abort 'Bash must not run in POSIX mode, Please unset POSIXLY_CORRECT and try again'
fi

usage() {
	cat << EndOfStream
	Homebrew Installer
	Usage: [MONINTERACTIVE=1] [CI=1] install.sh [options]
		-h, --help 		Display this help docs
		NONINTERACTIVE 	Install without prompting for user input
		CI 				Install in CI mode (e.g. do not prompt for user input)

EndOfStream
	exit "${1:-0}" # exit 1 or 0 ?
}

# Docs:
# Args parser for this script
while [[ $# -gt 0 ]] # args.len > 0
do
	case "$1" in
		-h | --help) usage ;;
		*)
			warn "Unsupported or unrecognized option: '$1'"
			usage -h
			;;
	esac
done

# string formatters, is this making function ?
if [[ -t 1 ]]; then
	tty_escape() { printf "\033[%sm" "$1"; }
else
	tty_escape() { :; }
fi

tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"

shell_join() {
	local arg
	printf "%s" "$1" # print this script first args
	shift # what the fuck is this
	for arg in "$@" # for arg in args
	do
		printf " "
		printf "%s" "${arg// /\ }"
	done
}


chomp() {
	printf "%s" "${1/"$'\n'"/}"
}

ohai() {
	printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "${shell_join "$@"}"
}

warn() {
	printf "${tty_red}Warning${tty_reset}: %s\n" "${chomp "$1"}" >&2 # stderr
}


# Docs:
# Check if script is run non-interactively (e.g. CI)
# If it is run non-interactively we should not prompt for passwords
# Always use single-quoted strings with `exp` expressions
# shellcheck disable=SC2016
if [[ -z "${NONINTERACTIVE-}" ]]; then
	if [[ -n "${CI-}" ]]; then
		warn 'Running in non-interactive mode because `$CI` is set'
		NONINTERACTIVE=1
	elif [[ ! -t 0 ]]; then
		if [[ -z ]]
